package com.example.understand_compose1.testcase_activity

import android.content.DialogInterface
import android.os.Bundle
import android.view.View
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.example.understand_compose1.R
import com.example.understand_compose1.databinding.ActivityFetAlertDialogBinding
import org.w3c.dom.Text
import kotlin.math.sin
import kotlin.system.exitProcess

class FetAlertDialogActivity : AppCompatActivity() {
    lateinit var binding: ActivityFetAlertDialogBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFetAlertDialogBinding.inflate(layoutInflater)
        setContentView(binding.root)

        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }


    }


    fun showNormalDialog(view: View) {
        val handlenegativeclick = object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface?, which: Int) {
                dialog?.cancel()
            }
        }

        val handlepositiveclick = object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface?, which: Int) {
                exitProcess(0)
            }
        }

        val ad = AlertDialog.Builder(this)
            .setTitle("温馨提示")
            .setMessage("确定要推出吗")
            .setNegativeButton(
                "取消",
//                handlenegativeclick
                DialogInterface.OnClickListener { dialog, which ->
                    Toast.makeText(this, "H", Toast.LENGTH_SHORT).show()
                }
            )
            .setPositiveButton(
                "确定",
                handlepositiveclick
            )
            .create()
        ad.show()
    }


    private val fontsizeArray = arrayOf("小号", "默认", "大号", "超大号")
    private val textsizeArray = arrayOf(10, 20, 30, 40, 50)
    private var singlecheckindex: Int = 0;

    fun showSingleChoice(view: View) {
        val ad = AlertDialog.Builder(this)
            .setTitle("请选择字体大小")
            .setSingleChoiceItems(
                fontsizeArray,
                1,
                DialogInterface.OnClickListener {
//                    dialog, which ->
//                    BUG: which 初始值= -1
//                        singlecheckindex =
                        dialog: DialogInterface?, which: Int ->
                    singlecheckindex = which
                })
            .setPositiveButton("确定",
                DialogInterface.OnClickListener {
//                     BUG: 这里马上奔溃, -1 out of index??
//                     FIXED: toFloat()修复bug
                        dialog, which ->
                    val tv1 = binding.tv1
                    tv1.textSize = textsizeArray[singlecheckindex].toFloat()
                }
            )
            .setNegativeButton("取消", null)
            .create().show()
    }
}